package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.HnsbDataResult;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.kzgs.KzgsResultJson;
import com.app.domain.kzgs.Record;
import com.app.domain.kzgs.RecordDetails;
import com.app.domain.wl.WlYjData;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import com.app.utils.TimeHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class HnsbService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public List<WlYjData> getWlYjData(List<YjData> yjDataList, UploadParameter uploadParameter, String vendorno, String deviceno, String platformdeviceid) {
        List<WlYjData> wlYjDataList = new ArrayList<>();
        String componentid = UUID.randomUUID().toString().replace("-", "");
        for (YjData yjData : yjDataList) {
            WlYjData wlYjData = new WlYjData();
            wlYjData.setId(UUID.randomUUID().toString().replace("-", ""));
            wlYjData.setTaskNo(deviceno+yjData.getBridge_code());
            wlYjData.setPlatformdeviceid(platformdeviceid);
            wlYjData.setVendorno(vendorno);
            wlYjData.setDeviceno(deviceno);
            wlYjData.setComponentid(componentid);
            wlYjData.setComponentparts(yjData.getBridge_code());
            wlYjData.setEngineeringname(uploadParameter.getGcmc());
            wlYjData.setEngineeringsite(uploadParameter.getSgbw());
            wlYjData.setBeamtype(uploadParameter.getLblx());
            wlYjData.setConcretestrength(uploadParameter.getConcretestrength());
            wlYjData.setModulusofelasticity(0);
            wlYjData.setSteelbeamno(yjData.getDuct_code());
            wlYjData.setSteelstrand("");
            wlYjData.setPulpingorientation(uploadParameter.getYajiangfangxiang());
            wlYjData.setPulpingorder(uploadParameter.getYajiangbuzhou());
            wlYjData.setFlowvelocity(yjData.getPractical_fluidity());
            wlYjData.setFluidity(yjData.getPractical_fluidity());
            wlYjData.setMixproportion(uploadParameter.getPhb());
            wlYjData.setWaterratio(Float.toString(yjData.getWater_binder_ratio()));
            wlYjData.setStirringtime(uploadParameter.getStirtime());
            wlYjData.setStartdate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            wlYjData.setEnddate(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            wlYjData.setVacuumdegree("");
            wlYjData.setCycletime("");
            wlYjData.setStabilizingpressure(Float.toString(yjData.getStabilize_pressure()));
            wlYjData.setPulpingvolume(Float.toString(yjData.getPractical_volume()));
            wlYjData.setStandardvolume(Float.toString(yjData.getTheory_volume()));
            wlYjData.setMeasuretype("2");
            wlYjData.setPulpingpressurein(Float.toString(yjData.getMudjack_pressure()));
            wlYjData.setPulpingpressureout(Float.toString(yjData.getStabilize_pressure()));
            wlYjData.setHoldingtime(yjData.getPressurize_time());
            Map<String, List> graphData = yjDataService.getNewGraphData2(String.valueOf(yjData.getId()));
            List<String> mcgList = graphData.get("mcgList");
            List<String> mcgOutList = graphData.get("mcgOutList");
            List<Float> mcgVolumeInList = graphData.get("mcgVolumeInList");
            List<String> mcgVolumeList = mcgVolumeInList.stream().map(Object::toString).collect(Collectors.toList());
            wlYjData.setVolumecurve(String.join(",",mcgVolumeList));
            wlYjData.setPressureincurve(String.join(",",mcgList));
            wlYjData.setPressureoutcurve(String.join(",",mcgOutList));
            wlYjData.setResult("0");
            wlYjData.setUserid(uploadParameter.getMemo());
            wlYjData.setOtherinformation("/");
            wlYjData.setBirthtime(TimeHelper.parseDate1(new Date()));
            wlYjData.setHoletype("0");
            wlYjData.setGoutingmode("0");
            wlYjData.setIsAlarm("0");
            wlYjData.setAlarm_info("无");
            wlYjDataList.add(wlYjData);
        }
        return wlYjDataList;
    }


    public HnsbDataResult sendMessage(String serviceUrl, String json,Map map) {
        String result = HttpClients.requestPost(serviceUrl, json,map);
        HnsbDataResult resultJson = (HnsbDataResult) JSON.parseObject(StringUtil.replaceBlank(result), HnsbDataResult.class);
        return resultJson;
    }


}
